package com.mujin.keji.collectionsystem.permissions;


import com.tbruyelle.rxpermissions.Permission;

import java.util.ArrayList;
import java.util.List;

import rx.Observer;

/**
 * 权限观察者的封装
 * 申请多个权限，并对权限
 * 获取情况遍历
 *
 *
 * @author weishixiong
 * @Time 2018-04-23
 */
public abstract class PermissionsListObserver implements Observer<Permission> {
    private List<Permission> hasGetPermissions;
    private List<Permission> noGetPermissions;
    protected abstract void onHasGetPermissions(List<Permission> hasGetPermissions);

    protected abstract void onNoGetPermissions(List<Permission> noGetPermissions);

    protected abstract void onException(Throwable e);


    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
        onException(e);
    }

    @Override
    public void onNext(Permission permission) {
        hasGetPermissions = new ArrayList<>();
        noGetPermissions = new ArrayList<>();
        if (permission.granted) {
            hasGetPermissions.add(permission);
        } else {
            noGetPermissions.add(permission);
        }
        onHasGetPermissions(hasGetPermissions);
        onNoGetPermissions(noGetPermissions);
    }
}
